<template>
  <el-dialog
    title="新增权限点"
    :visible="showDialog"
    width="30%"
    :before-close="close"
    @close="close"
  >
    <el-form ref="pmsForm" :model="pmsForm" :rules="rules" label-width="80px">
      <el-form-item prop="name" label="权限名称">
        <el-input v-model="pmsForm.name" size="mini" style="width: 90%;" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="pmsForm.code" size="mini" style="width: 90%;" />
      </el-form-item>
      <el-form-item prop="description" label="权限描述">
        <el-input v-model="pmsForm.description" type="textarea" :rows="3" size="mini" style="width: 90%;" />
      </el-form-item>
      <el-form-item prop="enVisible" label="开启">
        <el-switch v-model="pmsForm.enVisible" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :offset="6">
            <el-button size="mini" type="primary" @click="submitPms">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addPermissionApi, getPermissionDetailApi, updatePermissionApi } from '@/api/permission'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    pmsNameList: {
      type: Array,
      default: () => []
    },
    pmsCodeList: {
      type: Array,
      default: () => []
    },
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      pmsForm: {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }, {
          trigger: 'blur',
          validator: (rule, value, callback) => {
            if (this.pmsNameList.includes(value) && !this.pmsForm.id) {
              callback(new Error('权限名称已存在'))
            } else {
              callback()
            }
          }
        }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }, {
          trigger: 'blur',
          validator: (rule, value, callback) => {
            if (this.pmsCodeList.includes(value) && !this.pmsForm.id) {
              callback(new Error('权限标识已存在'))
            } else {
              callback()
            }
          }
        }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    // if (this.isEdit && this.id) {
    //   // isEdit为true，表示是编辑权限点,通过传过来的id来回显数据
    //   this.getPermissionDetail()
    // }
  },
  methods: {
    close() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 重置表单
      this.$refs.pmsForm.resetFields()
      // 清空表单数据
      this.pmsForm = {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      }
    },
    submitPms() {
      this.$refs.pmsForm.validate(async valid => {
        if (valid) {
          if (this.pmsForm.id) {
            // 如果有id，则表示是编辑权限点
            await updatePermissionApi(this.pmsForm)
          } else {
            // 如果没有id，则表示是添加权限点
            // 如果父级传过来的pid为0，则表示添加的是页面权限（type为1），否则是按钮权限（type为2）
            await addPermissionApi({
              ...this.pmsForm,
              type: this.id === 0 ? 1 : 2,
              pid: this.id
            })
          }
          this.$message.success(this.pmsForm.id ? '编辑成功' : '添加成功')
          this.close()
          // 通知父级重新获取数据
          this.$emit('refresh')
        }
      })
    },
    async getPermissionDetail(id) {
      // 回显数据
      this.pmsForm = await getPermissionDetailApi(id)
    }
  }
}
</script>

<style>

</style>
